package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwafT;
import cn.e4j.modular.crm.entity.GwaflT;
import cn.e4j.modular.crm.mapper.GwafTMapper;
import cn.e4j.modular.crm.service.GwafTService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  客户类型设置 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-01-11
 */
@Service
public class GwafTServiceImpl extends ServiceImpl<GwafTMapper, GwafT> implements GwafTService {

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwaflist(Page<Object> page, Dto dto) {

        List<Map<String, Object>> gwafDtos = this.baseMapper.listGwafPage(page, dto);

        return gwafDtos;
    }

    @Transactional(readOnly = true)
    public GwafT getByGwaf001(String ent, String gwaf001) {
        UpdateWrapper<GwafT> gwafTUpdateWrapper = new UpdateWrapper<>();
        gwafTUpdateWrapper.eq("GWAFENT",ent);
        gwafTUpdateWrapper.eq("GWAF001",gwaf001);
        GwafT gwaf = this.baseMapper.selectOne(gwafTUpdateWrapper);
        return gwaf;
    }

    @Transactional
    @Override
    public void add(GwafT gwaf,GwaflT gwafl) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwaf.setGWAFENT(shiroUser.getDBC());
        gwaf.setGWAF002(" ");
        gwaf.setGWAF003(" ");//预留栏位2
        gwaf.setGWAFUSER(shiroUser.getACCOUNT());
        gwaf.setGWAFGRUP(shiroUser.getORG_ID());
        gwaf.setGWAFDATE(LocalDate.now());
        gwaf.setGWAFTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        gwaf.setGWAFORIG(shiroUser.getORG_ID());
        gwaf.setGWAFORIU(shiroUser.getACCOUNT());

        gwafl.setGWAFLENT(shiroUser.getDBC());
        gwafl.setGWAFL001(gwaf.getGWAF001());
        gwafl.setGWAFL002(LangUtils.getLang(shiroUser.getLANG()));
        gwaf.insert();
        gwafl.insert();
    }

    @Transactional
    @Override
    public void edit(GwafT gwaf,GwaflT gwafl) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwaf.setGWAFMODU(shiroUser.getACCOUNT());
        UpdateWrapper<GwafT> gwafTUpdateWrapper = new UpdateWrapper<>();
        gwafTUpdateWrapper.eq("GWAFENT",gwaf.getGWAFENT());
        gwafTUpdateWrapper.eq("GWAF001",gwaf.getGWAF001());

        UpdateWrapper<GwaflT> gwaflTUpdateWrapper = new UpdateWrapper<>();
        gwaflTUpdateWrapper.eq("GWAFLENT",gwaf.getGWAFENT());
        gwaflTUpdateWrapper.eq("GWAFL001",gwaf.getGWAF001());

        gwaf.update(gwafTUpdateWrapper);
        gwafl.update(gwaflTUpdateWrapper);
    }


    @Transactional
    @Override
    public void delete(GwafT gwaf,GwaflT gwafl) {
        UpdateWrapper<GwafT> gwafTUpdateWrapper = new UpdateWrapper<>();
        gwafTUpdateWrapper.eq("GWAFENT",gwaf.getGWAFENT());
        gwafTUpdateWrapper.eq("GWAF001",gwaf.getGWAF001());

        UpdateWrapper<GwaflT> gwaflTUpdateWrapper = new UpdateWrapper<>();
        gwaflTUpdateWrapper.eq("GWAFLENT",gwaf.getGWAFENT());
        gwaflTUpdateWrapper.eq("GWAFL001",gwaf.getGWAF001());

        gwaf.delete(gwafTUpdateWrapper);
        gwafl.delete(gwaflTUpdateWrapper);
    }

    @Transactional
    @Override
    public void delDataBatch(List<GwafT> gwafTList) {
        for(GwafT gwafT : gwafTList){
            UpdateWrapper<GwafT> gwafTUpdateWrapper = new UpdateWrapper<>();
            gwafTUpdateWrapper.eq("GWAFENT",gwafT.getGWAFENT());
            gwafTUpdateWrapper.eq("GWAF001",gwafT.getGWAF001());

            GwaflT gwaflT = new GwaflT();
            UpdateWrapper<GwaflT> gwaflTUpdateWrapper = new UpdateWrapper<>();
            gwaflTUpdateWrapper.eq("GWAFLENT",gwafT.getGWAFENT());
            gwaflTUpdateWrapper.eq("GWAFL001",gwafT.getGWAF001());

            gwafT.delete(gwafTUpdateWrapper);
            gwaflT.delete(gwaflTUpdateWrapper);
        }
    }
}
